package com.pk.platform.util;



import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.constant.Constant;

public class PrivilegeValidator extends BodyTagSupport{
	
	private static final long serialVersionUID = 1L;
	private String moduleName;
	private String privilegeValue;
	private String privilegeId;
	public int doStartTag()throws JspException
	{ 
		if (privilegeId != null && !"".equals(privilegeId) )
		{
			int flag = SKIP_BODY;
			User u = (User)pageContext.getSession().getAttribute(Constant.USER);
	        Map<String, String> userPrivileges = new HashMap<String, String>();
	        if(u != null){
	           userPrivileges = u.getPrivilege();
	        }
	        
	        if (userPrivileges.get(privilegeId) != null) {
	            flag =  EVAL_BODY_INCLUDE;
                return flag;
	        }
	        return flag;
		} 
		else
		{
			throw new JspException("<my:hasPermission>标签的modelName和privilegeValue属性的值不能是空字符串!privilegeId的值必须是数字");
		}
	}

	public int doEndTag() throws JspException {
        return EVAL_PAGE;
	}

    
    
    
    public String getModuleName() {
        return moduleName;
    }
    
    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getPrivilegeValue() {
        return privilegeValue;
    }

    public void setPrivilegeValue(String privilegeValue) {
        this.privilegeValue = privilegeValue;
    }

    public String getPrivilegeId() {
        return privilegeId;
    }
    
    public void setPrivilegeId(String privilegeId) {
        this.privilegeId = privilegeId;
    }
}
